package com.example.javaairportflightanalysis.service.impl;

import com.example.javaairportflightanalysis.dao.DfmeBllsMapper;
import com.example.javaairportflightanalysis.domin.dto.DfmeBllsDto;
import com.example.javaairportflightanalysis.service.BaseBllsService;
import com.example.javaairportflightanalysis.utils.GroupAndPageUtil;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class BaseBllsServiceImpl implements BaseBllsService {
    @Resource
    DfmeBllsMapper dfmeBllsMapper;
    @Override
    public PageInfo searchLuggage(String fide, String ddtm, String code, Integer pageNum, Integer pageSize) throws ParseException {
        List<DfmeBllsDto> dfmeBllsDtos = dfmeBllsMapper.selectLuggage(fide, ddtm, code);
        List<DfmeBllsDto> mergeList = new ArrayList<>();
        for (int i = 0; i < dfmeBllsDtos.size(); i++) {
            DfmeBllsDto dfmeBllsDto = dfmeBllsDtos.get(i);
            setMergeList(mergeList, dfmeBllsDto);
        }
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(dfmeBllsDtos, pageNum, pageSize);
        return pageInfo;
    }

    private void setMergeList(List<DfmeBllsDto> mergeList, DfmeBllsDto dfmeBllsDto) throws ParseException {
        String ddtm1 = GroupAndPageUtil.getDate(dfmeBllsDto.getDdtm());
        dfmeBllsDto.setDdtm(ddtm1);
        String estr1 = GroupAndPageUtil.getDate(dfmeBllsDto.getEstr());
        dfmeBllsDto.setEstr(estr1);
        String eend1 = GroupAndPageUtil.getDate(dfmeBllsDto.getEend());
        dfmeBllsDto.setEend(eend1);
        String rstr1 = GroupAndPageUtil.getDate(dfmeBllsDto.getRstr());
        dfmeBllsDto.setRstr(rstr1);
        String rend1 = GroupAndPageUtil.getDate(dfmeBllsDto.getRend());
        dfmeBllsDto.setRend(rend1);
        String fide1 = dfmeBllsDto.getFide();
        String regex = "([A-Z0-9]+)-([0-9]+)-.*";
        String fidesub = fide1.replaceAll(regex, "$1$2");

        dfmeBllsDto.setFide(fidesub);
        mergeList.add(dfmeBllsDto);
    }

    /**
     * 查询行李信息
     */
    @Override
    public PageInfo getLuggage(Integer pageNum, Integer pageSize) throws ParseException {
        List<DfmeBllsDto> dfmeBllsDtos = dfmeBllsMapper.getLuggage();
        List<DfmeBllsDto> mergeList = new ArrayList<>();
        for (DfmeBllsDto dfmeBllsDto : dfmeBllsDtos) {
            setMergeList(mergeList, dfmeBllsDto);
        }

        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(mergeList, pageNum, pageSize);
        return pageInfo;
    }

}
